package com.verbetia.musictrends.persistence.hsqldb;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

public class Utils {
	
	
	public static ArrayList<String[]> rs2Strings(ResultSet rs)
	 throws SQLException
	{
	     // are implementation dependent unless you use the SQL ORDER statement
        ResultSetMetaData meta   = rs.getMetaData();
        int               colmax = meta.getColumnCount();
        int               i;
        Object            o = null;
        ArrayList<String[]> results = new ArrayList<String[]>();          
        //String[] item = new String[colmax];

        // the result set is a cursor into the data.  You can only
        // point to one row at a time
        // assume we are pointing to BEFORE the first row
        // rs.next() points to next row and returns true
        // or false if there is no next row, which breaks the loop
        for (; rs.next(); ) {
            
        	String[] item = new String[colmax];
            
            for (i = 0; i < colmax; ++i) {
                o = rs.getObject(i + 1);    // Is SQL the first column is indexed

                // with 1 not 0
                System.out.print(o.toString() + " ");
                item[i] = o.toString();
            }

            results.add(item);
            System.out.println(" ");
        }
        
        return results;
		
	}

	
	public static ArrayList<MusicEntity> rs2MusicEntity(ResultSet rs)
	 throws SQLException
	{
	     // are implementation dependent unless you use the SQL ORDER statement
       ResultSetMetaData meta   = rs.getMetaData();
       int               colmax = meta.getColumnCount();
       int               i;
       Object            o = null;
       ArrayList<MusicEntity> results = new ArrayList<MusicEntity>();          

       System.out.println(colmax);
       
       for (; rs.next(); ) {
    	   results.add(
    			  
           MusicEntity.create(rs.getString(1), 
        		   			  rs.getString(2),
        		   			  rs.getString(3),
        		   			 (rs.getString(4)==null)?"":rs.getString(4),
        		   			 (rs.getString(5)==null)?"":rs.getString(5),
        		   			 (rs.getString(6)==null)?"":rs.getString(6),
        		   			 (rs.getString(7)==null)?"":rs.getString(7))
        		   			
        		   			 
    	   );
           
           
       

           
           System.out.println(" ");
       }
       
       return results;
		
	}
	
	   public static String HTMLEntityEncode( String s )
	   {
	       StringBuffer buf = new StringBuffer();
	       int len = (s == null ? -1 : s.length());

	       for ( int i = 0; i < len; i++ )
	       {
	           char c = s.charAt( i );
	           if ( c>='a' && c<='z' || c>='A' && c<='Z' || c>='0' && c<='9' )
	           {
	               buf.append( c );
	           }
	           else
	           {
	               buf.append( "&#" + (int)c + ";" );
	           }
	       }
	       return buf.toString();
	   }

}
